package com.yuzhihao.writejvm.attributes.runtimeva;

import com.yuzhihao.writejvm.clazz.ClassInfo;
import com.yuzhihao.writejvm.read.Parse;
import com.yuzhihao.writejvm.read.ReadClassFileFatory;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.log4j.Log4j2;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 元素值属性
 *
 * @author yzh
 * @since 2021年12月11日14:28:03
 */
@Data
@Log4j2
@EqualsAndHashCode(callSuper = true)
public class ArrayValue extends ElementValue {

    private int num_values_u2 = 2;

    private int num_values;

    private List<ElementValue> evs = new ArrayList<>();

    public ArrayValue(int tag){
        super();
        super.setTag(tag);
    }

    @Override
    public void parse(DataInputStream dis, ClassInfo ci) {
        try {
            this.num_values = Parse.byteToInt(this.num_values_u2, dis);

            int i = 0;
            while (i < this.num_values){
                char item = (char) Parse.byteToInt(ElementValue.tag_u1, dis);
                ElementValue ev = ReadClassFileFatory.ev_instance(item);
                ev.parse(dis, ci);
                this.evs.add(ev);
                i++;
            }
        } catch (IOException e) {
            log.error(e);
        }
    }

}
